﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace Nirvana
{
    [CustomEditor(typeof(UIVariableBindText))]
    public sealed class UIVariableBindTextEditor : Editor
    {
        private SerializedProperty variableTable;
        private SerializedProperty format;
        private SerializedProperty paramBinds;
        private ReorderableList reorderableList;

        private void OnEnable()
        {
            if (this.target == null)
            {
                return;
            }
            SerializedObject serializedObject = base.serializedObject;
            this.variableTable = serializedObject.FindProperty("uiVariableTable");
            this.format = serializedObject.FindProperty("format");
            this.paramBinds = serializedObject.FindProperty("paramBinds");
            this.reorderableList = new ReorderableList(serializedObject, this.paramBinds);
            reorderableList.drawHeaderCallback = DrawHeader;//绘制列表表头
            reorderableList.elementHeight = 1f * EditorGUIUtility.singleLineHeight;
            reorderableList.drawElementCallback = DrawNameElement;//绘制每个元素的回调

        }

        public override void OnInspectorGUI()
        {
            UIVariableBind uivariableBind = (UIVariableBind)this.target;
            UIVariableTable variableTable = uivariableBind.VariableTable;
            if (variableTable == null)
            {
                EditorGUILayout.HelpBox("There is no EventTable in parent.", MessageType.Error);
            }
            base.serializedObject.Update();
            EditorGUILayout.PropertyField(this.variableTable, new GUILayoutOption[0]);
            EditorGUILayout.PropertyField(this.format, new GUILayoutOption[0]);
            this.reorderableList.DoLayoutList();
            base.serializedObject.ApplyModifiedProperties();
        }


        private  void DrawHeader(Rect rect)
        {
            GUI.Label(rect, "Param Binds:");
        }
        private void DrawNameElement(Rect rect, int index, bool isActive, bool isFocused)
        {
            SerializedProperty arrayElementAtIndex = this.paramBinds.GetArrayElementAtIndex(index);
            EditorGUI.PropertyField(rect, arrayElementAtIndex);
        }
    }
}


